package edu.uc.labs.MobilePrinting;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;

/**
 *
 * @author Matt Silbernagel
 */
public class Configurator {

    // Static	
	private static Level logLevel = Level.DEBUG;
    private static String os = System.getProperty("os.name");    
    private static String homeDir = System.getProperty("user.home");
    
	////////////////////////////////////////////
    // CONSTANTS
    ////////////////////////////////////////////

    // SLEEP
    final public static String HTTP_SERVER = "yeti.uclab.uc.edu";
    final static int SLEEP = 10000;
    final static int SLEEP_LONG = 30000;
    
    // FILES AND DIRS
    final public static String LOGS = "logs";
    final public static String PPD = "ppd";
    final public static String CACHE = "cache";
    final public static String ICONS = "icons";
    final public static String DRIVERS = "drivers";        
    
    final public static String XMLFILE = "printers.xml";
    final public static String LOGFILE = "mobilePrinting.log";    
    final public static String TRAYICON = "MP_Tray.gif";
    final public static String TRAYICON_PROGRESS = "MP_Progress_0.gif";
       
    final public static String DELIM = getDelim();
    final public static String CACHEDIR = getCache();
    
    final public static String OS = System.getProperty("os.name");
    
    // PRINTER, PRINTER SCRIPTS, ETC
    final public static String PORTNAME = "UCitMobilePrinter";
    final public static String PRINTERNAME = "UCitMobilePrinter";
	final public static String WINSCRIPTSPATH = getPrinterScriptsPath();
	final public static Logger LOGGER = setLogging();
    
    public Configurator(){    	
    	
    	
        
    }
    
    private static String getDelim(){
    	String delim = null;    	
    	delim = (os.equalsIgnoreCase("Linux")) || (os.equalsIgnoreCase("Mac OS X") || (os.equalsIgnoreCase("SunOS"))) ? "/" : "\\";
    	return delim;
    }
    
    private static String getCache(){
    	String dir = null;
    	if (os.equalsIgnoreCase("Linux") || os.equalsIgnoreCase("SunOS")){
    		dir = homeDir + "/.ucit/MobilePrinting/";
    	} else if(os.equalsIgnoreCase("Mac OS X")){
    		dir = homeDir + "/Library/Application Support/UCit/MobilePrinting/";
    	} else {
    		dir = homeDir + "\\Application Data\\ucit\\MobilePrinting\\";
    	}
    	return dir;
    }
    
    private static String getPrinterScriptsPath(){
    	String dir = "";
    	if (os.equalsIgnoreCase("Linux") || os.equalsIgnoreCase("Mac OS X") || os.equalsIgnoreCase("SunOS")) {
    		dir = "";
    	}else if (os.equalsIgnoreCase("Windows XP")) {
    		dir = "C:\\Windows\\system32";
    	}else if (os.equalsIgnoreCase("Windows 7")){
    		dir = "C:\\WINDOWS\\system32\\Printing_Admin_Scripts\\en-US";
    	}
    	return dir;
    	
    }
    
    private static Logger setLogging(){
    	Logger log = Logger.getLogger("MobilePrinting");
    	try{
    	
    		PatternLayout layout = new PatternLayout("%d - %c - %p - %m%n");
    		RollingFileAppender R = new RollingFileAppender(layout, CACHEDIR + LOGS + DELIM + LOGFILE, false);
    		R.setMaxFileSize("500KB");
    		R.setMaxBackupIndex(1);
        
    		ConsoleAppender c = new ConsoleAppender(layout);
    		log.addAppender(R);
    		log.addAppender(c);
    		log.setLevel(logLevel);
        } catch (Exception e){
        	System.out.println(e.toString());
    	}
		return log;
    	
    }
    
    public Logger getLogger(){
    	return LOGGER;
    }
    
    public String getOS(){
    	return os;
    }
    
    public String getHome() {
    	return homeDir;
    }
}
